package com.example.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.example.common.enums.RiderStatusEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Rider;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.RiderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * null业务处理
 **/
@Service
public class RiderService {

    @Resource
    private RiderMapper riderMapper;

    @Resource
    private UserService userService;

    /**
     * 新增
     */
    public void add(Rider rider) {
        riderMapper.insert(rider);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        Rider rider = this.selectById(id);
        Integer userId = rider.getUserId();
        User user = userService.selectById(userId);
        user.setRole("USER");
        userService.updateById(user);
        riderMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
           this.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Rider rider) {
        riderMapper.updateById(rider);
    }

    /**
     * 根据ID查询
     */
    public Rider selectById(Integer id) {
        return riderMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Rider> selectAll(Rider rider) {
        return riderMapper.selectAll(rider);
    }

    /**
     * 分页查询
     */
    public PageInfo<Rider> selectPage(Rider rider, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Rider> list = riderMapper.selectAll(rider);
        return PageInfo.of(list);
    }

    /**
     * 前端提交审核新增
     * @param rider
     */
    public void submitAdd(Rider rider) {
        if (BeanUtil.isEmpty(rider.getCardImage())){
            throw new CustomException("500","请填充您的身份证照片");
        }
        if (BeanUtil.isEmpty(rider.getCardNumber())){
            throw new CustomException("500","请输入您的身份证号码");
        }
        Integer userId = rider.getUserId();
        List<Rider> riderList = riderMapper.selectByUserId(userId);
        if (CollUtil.isNotEmpty(riderList)){
            throw new CustomException("500","本用户已经提交申请，请勿重复提交");
        }
        rider.setBalance(BigDecimal.ZERO);
        rider.setStatus(RiderStatusEnum.WAIT.getValue());

        riderMapper.insert(rider);
        User user = new User();
        user.setId(userId);
        user.setMessage("骑手资质审核已提交，请等待管理员审批");
        userService.updateById(user);
    }

    /**
     * 用户申请成为骑手审核通过校验逻辑
     * @param rider
     */
    @Transactional
    public void passExamine(Rider rider) {
        String status = rider.getStatus();
        if (status.equals(RiderStatusEnum.WAIT.getValue())){
            User user = new User();
            user.setId(rider.getUserId());
            user.setRole("RIDER");
            user.setMessage("");
            userService.updateById(user);

            rider.setStatus(RiderStatusEnum.PASS.getValue());
            riderMapper.updateById(rider);
        }
    }

    /**
     * 用户申请为骑手审核驳回逻辑
     * @param rider
     */
    @Transactional
    public void refuseExamine(Rider rider) {
        if (rider.getStatus().equals(RiderStatusEnum.WAIT.getValue())){
            User user = new User();
            user.setId(rider.getUserId());
            user.setRole("USER");
            user.setMessage("申请骑手资格已被驳回请重新申请");
            userService.updateById(user);

            riderMapper.deleteById(rider.getId());
        }
    }

    /**
     * 根据
     * @param riderUserId
     * @return
     */
    public Rider selectOneByUserId(Integer riderUserId) {
      return riderMapper.selectOneByUserId(riderUserId);
    }
}